﻿function Post()
{
    
    this.validatePost = function(storyURL, title, category, description, tags)
    {
        var storyMessage = $("#storyMessage");
        storyMessage.show();
        if(storyURL == null || storyURL == "")
        {
            storyMessage.attr("innerText", "Please input story URL");
            return false;
        }
        else if(title == null || title == "" )
        {
            storyMessage.attr("innerText", "Please input story Title");
            return false;
        }
        else if( category == null || category == -1 )
        {
            storyMessage.attr("innerText", "Please select the story category");
            return false;
        }
        else if( description == null || description == "" )
        {
            storyMessage.attr("innerText", "Please input story's description");
            return false;
        }
        
        return true;
    }

    
    this.comment = function(postId)
    {
        var author = $("#txtAuthor").val();
        if( author == null || author == "")
        {
            $("#commentMessage").show();
            $("#commentMessage").attr("innerText","please input your name");
            return;
        }
        var content = $("#txtComment").val();
        if( content == null || content == "")
        {
            $("#commentMessage").show();
            $("#commentMessage").attr("innerText","please write your opinion");
            return;
        }
        else{
            $("#txtComment").hide();
            $("#commentMessage").show();
            $("#commentMessage").attr("innerText","please wait while submiting your comment");
            $("#name").hide();
            $("#btnSubmitComment").attr("disabled","disabled");
        }
        $.ajax({
                type:"POST",
                url:"/Post/SubmitComment",
                data:"postID="+postId+"&content="+content+"&author="+author,
                success:function(comment){
                    comment = comment.parseJSON();
                    
                    var div = document.createElement("div");
                    if( comment["IsOdd"] == true )
                        div.setAttribute("className", "comment odd");
                    else
                        div.setAttribute("className", "comment even");
                        
                    var subDiv1 = document.createElement("div");
                    subDiv1.innerHTML = comment["Content"];
                    
                    div.appendChild(subDiv1);
                    
                    $("#comments").append(div);
                    $("#commentMessage").attr("innerText","Thanks for your comment");
                }
            });        
    }
    
    
    
    this.submitStory = function()
    {
        var storyURL = $("#txtStoryUrl").val();
        var title = $("#txtStoryTitle").val();
        var category = $("#ddlStoryCategory").val();
        var description = $("#txtStoryDescription").val();
        var tags = $("#txtStoryTags").val();
        
        if( this.validatePost(storyURL, title, category, description, tags) == true )
        {
            $.ajax({
                type:"POST",
                url:"/Post/New",
                data:"storyURL="+storyURL+"&title="+title+"&description="+description+"&categoryID="+category+"&tags="+tags,
                success:function(msg){
                    $("#storyMessage").attr("innerText", "Your post will coming soon");
                    window.setTimeout("", 2000);                                      
                    window.location.reload();         
                }
              });
        }
    }
        
    this.kick = function(postId)
    {
        $.ajax(
          {
            type:"POST",
            url:"/Post/Kick",
            data:"postID="+postId,
            success:function(msg){
                msg = msg.parseJSON();
                var kickID = "#Kick" + postId;
                $(kickID).attr("innerText", msg["KickCount"]);                
            }
          }  
        );
    }
}



